﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Xml;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using bmapi.SQLServerAccess;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using static bmapi.HelperClass.AssistFun;

namespace bmapi.Controllers
{
    public class CADApplyController : CADController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">流程id</param>
        /// <param name="process">工序编号</param>
        /// <param name="name">流程名</param>
        /// <param name="pod">pod文件名</param>
        /// <returns></returns>
        protected string ReturnOK(int id, int process, string name, int pod = 0)
        {
            WriteDebug("ReturnOK", "流程-"+id+ "工序编号-"+process + "流程名-" + name + "pod文件名-" + pod);
            XmlDocument doc = new XmlDocument();

            XmlNode root = doc.CreateElement("login");
            doc.AppendChild(root);

            XmlElement p = doc.CreateElement("status");
            p.InnerText = "200";
            root.AppendChild(p);

            p = doc.CreateElement("tip");
            p.InnerText = "OK";
            root.AppendChild(p);

            p = doc.CreateElement("id");
            p.InnerText = id.ToString();
            root.AppendChild(p);

            string sql = "select Model_tb.name from Model_tb join PC_tb on Model_tb.id=PC_tb.model_id where pc_id=@pcid";
            SQLCommand sc = new SQLCommand(sql, "@pcid", id);
            string modelName = sc.ExecuteScalar();
            if (modelName == null)
                modelName = "";
            p = doc.CreateElement("model");
            p.InnerText = modelName;
            root.AppendChild(p);

            p = doc.CreateElement("name");
            p.InnerText = name;
            root.AppendChild(p);

            p = doc.CreateElement("process");
            p.InnerText = process.ToString();
            root.AppendChild(p);

            p = doc.CreateElement("pod");
            p.InnerText = pod.ToString();
            root.AppendChild(p);

            return ConvertXmlToString(doc);
        }
        /// <summary>
        /// 获得一个流程，分给他
        /// </summary>  
        /// <param name="u"></param>
        /// <returns></returns>
        protected PC LockTask(User u)
        {
            string sql = "";
            SQLCommand sc = null;
            try
            {
                DataTable dt = new DataTable();
                PC pc = new PC();
                //查看有没有预分配给我的任务
                for (int i = 0; i < 5; i++)
                {
                    sql = "select * from PC_tb where station=@loginid and download=0 ";
                    sc = new SQLCommand(sql, "@loginid", u.login_id);
                    dt = sc.ExecuteScalars();
                    if (dt == null)
                        break;
                    pc = DataTableToList<PC>(dt)[0];
                    pc.maxStep = GetMax(pc.maxStep, pc.step);
                    sql = "update PC_tb set download=1,maxStep=@max where pc_id=@pcid";
                    sc = new SQLCommand(sql);
                    sc.Add("@max", pc.maxStep);
                    sc.Add("@pcid", pc.pc_id);
                    int rtn = Convert.ToInt32(sc.ExecuteNonQuery());
                    if (rtn == 1)
                        return pc;
                }
                //查看有没有适合的自由任务  
                for (int i = 0; i < 5; i++)
                {
                    sql = "select * from PC_tb where step=@ucode and download=0 and station=0 and PC_tb.PLID=@plid and (PC_tb.grouping=0 or @ugroup=0 or PC_tb.grouping=@ugroup)";
                    sc = new SQLCommand(sql);
                    sc.Add("@ucode", u.code);
                    sc.Add("@plid", u.PLID);
                    sc.Add("@ugroup", u.grouping);
                    dt = sc.ExecuteScalars();
                    if (dt == null)
                        return null;
                    pc = DataTableToList<PC>(dt)[0];
                    pc.maxStep = GetMax(pc.maxStep, pc.step);
                    sql = "update PC_tb set download=1,station=@loginid,maxStep=@max where station=0 and pc_id=@pcid";
                    sc = new SQLCommand(sql);
                    sc.Add("@loginid", u.login_id);
                    sc.Add("@max", pc.maxStep);
                    sc.Add("@pcid", pc.pc_id);
                    int rtn = Convert.ToInt32(sc.ExecuteNonQuery());
                    if (rtn == 1)
                        return pc;
                }
                //不能为用户找到合适的任务，并不是件大事，因为本来就有没事做的可能 
                return null;
            }
            catch (Exception e)
            {
                WriteLog("LockTask",e.Message);
                return null;
            }
            
        }
        /// <summary>
        /// 为员工寻找一个适合她的排料（裁剪规划）任务
        /// </summary>
        /// <param name="u"></param>
        /// <param name="code"></param>
        protected void LockMark(User u)
        { }
        /// <summary>
        /// 寻找待分配的裁床任务，寻找待分配的裁床任务(isCut=false铺布任务) 特体检查和裁床任务不能兼职，也不考虑返工约束
        /// </summary>
        /// <param name="u"></param>
        /// <param name="code"></param>
        protected void LockBed(int machineID, bool isCut)
        { }
    }
}
